import Vue from 'vue'
import Router from 'vue-router'
import index from '@/pages/index.vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(Router)

const routes = [{
    path: '/',
    name: 'index',
    component: index,
    meta: {
      index: 1,
      transition: 'fade'
    }
  },
  // 首页介绍
  {
    path: '/index/introduce',
    name: 'introduce',
    component: () => import('@/pages/introduce/index.vue'),
    meta: {
      transition: 'fade'
    },
    children: [{
        path: '/index/introduce/historyIntroduce',
        name: 'historyIntroduce',
        component: () => import('@/pages/introduce/introduce/historyIntroduce.vue'),
        meta: {
          transition: 'fade'
        },
      },
      {
        path: '/index/introduce/famous',
        name: 'famous',
        component: () => import('@/pages/introduce/introduce/famous.vue'),
        meta: {
          transition: 'fade'
        },
      },
      {
        path: '/index/introduce/culturalHeritage',
        name: 'culturalHeritage',
        component: () => import('@/pages/introduce/introduce/culturalHeritage.vue'),
        meta: {
          transition: 'fade'
        },
      },
      {
        path: '/index/introduce/villageHistory',
        name: 'villageHistory',
        component: () => import('@/pages/introduce/introduce/villageHistory.vue'),
        meta: {
          transition: 'fade'
        },
      },
      {
        path: '/index/introduce/villageHistory/villageId',
        name: 'villageId',
        component: () => import('@/pages/introduce/introduce/villageHistory/villageId.vue'),
        meta: {
          transition: 'fade'
        },
      }
    ]
  },
  {
    path: '/foods',
    name: 'foods',
    component: () => import('@/pages/foods'),
    meta: {
      index: 2,
      transition: 'fade'
    }
  },
  {
    path: '/news',
    name: 'news',
    component: () => import('@/pages/news'),
    meta: {
      index: 3,
      transition: 'fade'
    }
  },
  {
    path: '/folkloric',
    name: 'folkloric',
    component: () => import('@/pages/folkloric'),
    meta: {
      index: 4,
      transition: 'fade'
    }
  },
  // 购买美食
  {
    path: '/foodsbuy',
    name: 'foodsbuy',
    component: () => import('@/pages/foodsbuy/foodsBuy'),
    meta: {
      index: 2.5,
      transition: 'fade'
    }
  },
  // login
  {
    path: '/login',
    name: 'login',
    component: () => import('@/pages/login/login'),
  },
  {
    path: '/admin',
    name: 'admin',
    component: () => import('@/pages/admin/admin'),
    meta: {
      requireAuth: true
    },
    children: [{
        path: '/admin/index',
        name: 'AdminIndex',
        component: () => import('@/pages/admin/adminContent/adminIndex.vue'),
        meta: {
          requireAuth: true
        },
      },
      {
        path: '/admin/lunbotu',
        name: 'adminlunbotu',
        component: () => import('@/pages/admin/adminContent/lunbotu.vue'),
        meta: {
          requireAuth: true
        },
      },
      {
        path: '/admin/afoods',
        name: 'adminafoods',
        component: () => import('@/pages/admin/adminContent/adminfoods'),
        meta: {
          requireAuth: true
        },
      },
      {
        path: '/admin/abeauty',
        name: 'adminabeauty',
        component: () => import('@/pages/admin/adminContent/adminbeauty'),
        meta: {
          requireAuth: true
        },
      },
      {
        path: '/admin/afolklonric',
        name: 'adminabeauty',
        component: () => import('@/pages/admin/adminContent/adminfolklonric'),
        meta: {
          requireAuth: true
        },
      },
    ]
  }
]
const router = new VueRouter({
  mode: "history",
  routes
})
export default router
// export default new Router({
//   routes: [
//   ]
// })

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (store.state.token) { // 通过vuex state获取当前的token是否存在
      next();
    } else {
      next({
        path: '/login',
        query: {
          redirect: to.fullPath
        } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }

  } else {
    next();
  }
})
